package com.fire.web.modules.all.utils;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class WrapperUtil {
	//遍历参数返回查询条件
	public static EntityWrapper getEw(Map<String, Object> params){
		EntityWrapper ew = new EntityWrapper();
		for (Map.Entry<String, Object> entry : params.entrySet()) {
			//去除limit和page参数，该参数属于分页参数
			if(entry.getKey().equals("limit")||entry.getKey().equals("page")){
				continue;
			}
			if(StringUtils.isNotBlank(entry.getValue().toString())){
				ew.like(entry.getKey(),entry.getValue().toString());
			}
		}
		return ew;
	}

	/**
	 * 拼接list内实体所有参数作为查询参数查询参数
	 * @param wrapper sqlSegment
	 * @param list 参数列表
	 * @param keyName 参数名
	 * @param valueName 查询对应列名
	 * @param joinStr 拼接符号"","or","and"
	 * @return 拼接好的sqlSegment
	 * @throws StringIndexOutOfBoundsException keyName和valueName为空或null
	 * @author zhongzifeng
	 * @date 2019-02-22 10:56
	 */
	public static <E> Wrapper<E> appendEwByEntity(Wrapper<E> wrapper, List<E> list, String keyName, String valueName, String joinStr) throws StringIndexOutOfBoundsException {
		if(StringUtils.isEmpty(keyName) || StringUtils.isEmpty(valueName)){
			throw new StringIndexOutOfBoundsException();
		}
		if(wrapper == null){
			throw new NullPointerException();
		}
		int i=0;
		for(E e : list){
			if(i==list.size()-1){
				joinStr = "";
			}
			wrapper = appendEqExByEntity(e,wrapper,valueName,joinStr);
			i++;
		}
		return wrapper;
	}

	/**
	 * list拼接已知参数名查询参数
	 * @param wrapper sqlSegment
	 * @param list 参数列表
	 * @param keyName 参数名
	 * @param valueName 查询对应列名
	 * @param joinStr 拼接符号"","or","and"
	 * @return 拼接好的sqlSegment
	 * @throws StringIndexOutOfBoundsException keyName和valueName为空或null
	 * @author zhongzifeng
	 * @date 2019-02-22 10:56
	 */
	public static <E> Wrapper<E> appendEw(Wrapper<E> wrapper, List<E> list, String keyName, String valueName, String joinStr) throws StringIndexOutOfBoundsException {
		if(StringUtils.isEmpty(keyName) || StringUtils.isEmpty(valueName)){
			throw new StringIndexOutOfBoundsException();
		}
		if(wrapper == null){
			throw new NullPointerException();
		}
		int i=0;
		for(E e : list){
			if(i==list.size()-1){
				joinStr = "";
			}
			wrapper = appendEqExByAttribute(e,wrapper,keyName,valueName,joinStr);
			i++;
		}
		return wrapper;
	}

	/**
	 * list拼接已知参数名查询参数
	 * @param list 参数列表
	 * @param keyName 参数名
	 * @param valueName 查询对应列名
	 * @param joinStr 拼接符号"","or","and"
	 * @return 拼接好的sqlSegment
	 * @throws StringIndexOutOfBoundsException keyName和valueName为空或null
	 * @author zhongzifeng
	 * @date 2019-02-22 10:56
	 */
	public static <E> Wrapper<E> appendEw( List<E> list, String keyName, String valueName, String joinStr) throws StringIndexOutOfBoundsException {
		Wrapper wrapper = new EntityWrapper();
		return appendEw(wrapper,list,keyName,valueName,joinStr);
	}

	protected  static <E> Wrapper<E> appendEqExByAttribute(E e,Wrapper<E> wrapper, String keyName, String valueName, String joinStr){
		try {
			Object objv = PropertyUtils.getProperty(e, keyName);
			String value = objv.toString();
			System.out.println(valueName+":"+value);
			wrapper = appedEqEx(wrapper,valueName,value,joinStr);
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		}
		return wrapper;
	}

	protected static <E> Wrapper<E> appendEqExByEntity(E e,Wrapper<E> wrapper, String valueName, String joinStr){
		Field[] fields = e.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			String nodeName = fields[i].getName().toString().toUpperCase();
			Class clazz = fields[i].getType();
			Object objv = null;
			try {
				// 过滤无意义参数
				if("serialVersionUID".equals(fields[i].getName())){
					continue;
				}
				objv = PropertyUtils.getProperty(e, fields[i].getName());
				String value = objv.toString();
				System.out.println(valueName+":"+value);
				// 过滤空值
				if(StringUtils.isEmpty(value)){
					continue;
				}
				wrapper = appedEqEx(wrapper,valueName,value,joinStr);
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (InvocationTargetException e1) {
				e1.printStackTrace();
			} catch (NoSuchMethodException e1) {
				e1.printStackTrace();
			}
		}
		return wrapper;
	}


	protected static <E> Wrapper<E> appedEqEx(Wrapper<E> wrapper, String valueName, Object value, String joinStr){
		wrapper.eq(valueName,value);
		if("or".equals(joinStr)){
			wrapper.or();
		}
		if("and".equals(joinStr)){
			wrapper.and();
		}
		return wrapper;
	}


}
